#include "gfx/legato/generated/screen/le_gen_screen_Screen0.h"
#include <p32xxxx.h>    //ys added          //レジスタ名など引き当ての為    //LATDbits.LATD0 = 1; 
#include "lcd_ACM1602_lib_i2c.h"

#include "definitions.h"    //必須 ys added //これがないと　SYS_TIME_CallbackRegisterMS()がコンパイエラー

// screen member widget declarations
leWidget* root0;

leWidget* Screen0_BackgroundPanel;
leLabelWidget* Screen0_LabelWidget0;
leLabelWidget* Screen0_LabelWidget1;
leLabelWidget* Screen0_LabelWidget2;
leImageWidget* Screen0_ImageWidget0;
leImageWidget* Screen0_ImageWidget1;
leImageWidget* Screen0_ImageWidget2;
leImageWidget* Screen0_ImageWidget3;
leButtonWidget* Screen0_NextBtn;

static leBool initialized = LE_FALSE;
static leBool showing = LE_FALSE;

extern SYS_TIME_HANDLE StringChange_handle;   //Scr1 タイトル文字切り替えタイマ
extern bool WideUseMojuleBoard;    //汎用モジュール評価ボードが接続されている場合：1


leResult screenInit_Screen0(void)
{
    if(initialized == LE_TRUE)
        return LE_FAILURE;

    initialized = LE_TRUE;

    return LE_SUCCESS;
}

leResult screenShow_Screen0(void)
{
    if(showing == LE_TRUE)
        return LE_FAILURE;

    // layer 0
    root0 = leWidget_New();
    root0->fn->setSize(root0, 800, 480);
    root0->fn->setBackgroundType(root0, LE_WIDGET_BACKGROUND_NONE);
    root0->fn->setMargins(root0, 0, 0, 0, 0);
    root0->flags |= LE_WIDGET_IGNOREEVENTS;
    root0->flags |= LE_WIDGET_IGNOREPICK;

    Screen0_BackgroundPanel = leWidget_New();
    Screen0_BackgroundPanel->fn->setPosition(Screen0_BackgroundPanel, 0, 0);
    Screen0_BackgroundPanel->fn->setSize(Screen0_BackgroundPanel, 800, 480);
    Screen0_BackgroundPanel->fn->setScheme(Screen0_BackgroundPanel, &BaseGreen_TextBlack);
    root0->fn->addChild(root0, (leWidget*)Screen0_BackgroundPanel);

    Screen0_LabelWidget0 = leLabelWidget_New();
    Screen0_LabelWidget0->fn->setPosition(Screen0_LabelWidget0, 251, 17);
    Screen0_LabelWidget0->fn->setSize(Screen0_LabelWidget0, 350, 40);
    Screen0_LabelWidget0->fn->setScheme(Screen0_LabelWidget0, &BaseGreen_TextBlack);
    Screen0_LabelWidget0->fn->setHAlignment(Screen0_LabelWidget0, LE_HALIGN_CENTER);
    Screen0_LabelWidget0->fn->setString(Screen0_LabelWidget0, (leString*)&string_Welcome_to);
    root0->fn->addChild(root0, (leWidget*)Screen0_LabelWidget0);

    Screen0_LabelWidget1 = leLabelWidget_New();
    Screen0_LabelWidget1->fn->setPosition(Screen0_LabelWidget1, 150, 58);
    Screen0_LabelWidget1->fn->setSize(Screen0_LabelWidget1, 550, 40);
    Screen0_LabelWidget1->fn->setScheme(Screen0_LabelWidget1, &BaseGreen_TextBlack);
    Screen0_LabelWidget1->fn->setHAlignment(Screen0_LabelWidget1, LE_HALIGN_CENTER);
    Screen0_LabelWidget1->fn->setString(Screen0_LabelWidget1, (leString*)&string_PIC32MZ_Evaluation_Board);
    root0->fn->addChild(root0, (leWidget*)Screen0_LabelWidget1);

    Screen0_LabelWidget2 = leLabelWidget_New();
    Screen0_LabelWidget2->fn->setPosition(Screen0_LabelWidget2, 230, 107);
    Screen0_LabelWidget2->fn->setSize(Screen0_LabelWidget2, 500, 35);
    Screen0_LabelWidget2->fn->setScheme(Screen0_LabelWidget2, &BaseGreen_TextBlack);
    Screen0_LabelWidget2->fn->setHAlignment(Screen0_LabelWidget2, LE_HALIGN_CENTER);
    Screen0_LabelWidget2->fn->setString(Screen0_LabelWidget2, (leString*)&string_INT070_inchi7_0_800x480pixel);
    root0->fn->addChild(root0, (leWidget*)Screen0_LabelWidget2);

    Screen0_ImageWidget0 = leImageWidget_New();
    Screen0_ImageWidget0->fn->setPosition(Screen0_ImageWidget0, 50, 162);
    Screen0_ImageWidget0->fn->setSize(Screen0_ImageWidget0, 240, 144);
    Screen0_ImageWidget0->fn->setBorderType(Screen0_ImageWidget0, LE_WIDGET_BORDER_NONE);
    Screen0_ImageWidget0->fn->setImage(Screen0_ImageWidget0, (leImage*)&Fuji240x144noJP_RGB);
    root0->fn->addChild(root0, (leWidget*)Screen0_ImageWidget0);

    Screen0_ImageWidget1 = leImageWidget_New();
    Screen0_ImageWidget1->fn->setPosition(Screen0_ImageWidget1, 50, 321);
    Screen0_ImageWidget1->fn->setSize(Screen0_ImageWidget1, 240, 144);
    Screen0_ImageWidget1->fn->setBorderType(Screen0_ImageWidget1, LE_WIDGET_BORDER_NONE);
    Screen0_ImageWidget1->fn->setImage(Screen0_ImageWidget1, (leImage*)&GojyuKouyou_240x144_RGB);
    root0->fn->addChild(root0, (leWidget*)Screen0_ImageWidget1);

    Screen0_ImageWidget2 = leImageWidget_New();
    Screen0_ImageWidget2->fn->setPosition(Screen0_ImageWidget2, 510, 162);
    Screen0_ImageWidget2->fn->setSize(Screen0_ImageWidget2, 240, 144);
    Screen0_ImageWidget2->fn->setBorderType(Screen0_ImageWidget2, LE_WIDGET_BORDER_NONE);
    Screen0_ImageWidget2->fn->setImage(Screen0_ImageWidget2, (leImage*)&SummerSea240x144_RGB);
    root0->fn->addChild(root0, (leWidget*)Screen0_ImageWidget2);

    Screen0_ImageWidget3 = leImageWidget_New();
    Screen0_ImageWidget3->fn->setPosition(Screen0_ImageWidget3, 510, 321);
    Screen0_ImageWidget3->fn->setSize(Screen0_ImageWidget3, 240, 144);
    Screen0_ImageWidget3->fn->setBorderType(Screen0_ImageWidget3, LE_WIDGET_BORDER_NONE);
    Screen0_ImageWidget3->fn->setImage(Screen0_ImageWidget3, (leImage*)&Kikansha_Yuki240x144_RGB);
    root0->fn->addChild(root0, (leWidget*)Screen0_ImageWidget3);

    Screen0_NextBtn = leButtonWidget_New();
    Screen0_NextBtn->fn->setPosition(Screen0_NextBtn, 330, 425);
    Screen0_NextBtn->fn->setSize(Screen0_NextBtn, 140, 40);
    Screen0_NextBtn->fn->setScheme(Screen0_NextBtn, &BaseYellow_TextBlue);
    Screen0_NextBtn->fn->setString(Screen0_NextBtn, (leString*)&string_NEXT);
    Screen0_NextBtn->fn->setPressedEventCallback(Screen0_NextBtn, event_Screen0_NextBtn_OnPressed);
    root0->fn->addChild(root0, (leWidget*)Screen0_NextBtn);

    leAddRootWidget(root0, 0);
    leSetLayerColorMode(0, LE_COLOR_MODE_RGB_565);

    showing = LE_TRUE;

    return LE_SUCCESS;
}





void event_Screen0_NextBtn_OnPressed(leButtonWidget* btn)   //NEXTボタンクリック伴うイベント
{
    SYS_TIME_TimerStop(StringChange_handle);    //タイトル文字切り替えタイマ停止

    legato_showScreen(screenID_Screen1);                    //screen1へ
    All_LedOff();   //全LED消灯
    
    if(WideUseMojuleBoard == true)  //汎用モジュール評価ボード（段積みボード)の接続有
    {  
        lcd_ACM1602_cmd_i2c(0x80);  //1行目の先頭へ
        lcd_ACM1602_str_i2c("LED TouchControl ");
        lcd_ACM1602_cmd_i2c(0xC0);  //2行目の先頭へ
        lcd_ACM1602_str_i2c("    LED: OFF     ");
    }
}

void screenUpdate_Screen0(void)
{
}

void screenHide_Screen0(void)
{

    leRemoveRootWidget(root0, 0);
    leWidget_Delete(root0);
    root0 = NULL;

    Screen0_BackgroundPanel = NULL;
    Screen0_LabelWidget0 = NULL;
    Screen0_LabelWidget1 = NULL;
    Screen0_LabelWidget2 = NULL;
    Screen0_ImageWidget0 = NULL;
    Screen0_ImageWidget1 = NULL;
    Screen0_ImageWidget2 = NULL;
    Screen0_ImageWidget3 = NULL;
    Screen0_NextBtn = NULL;


    showing = LE_FALSE;
}

void screenDestroy_Screen0(void)
{
    if(initialized == LE_FALSE)
        return;

    initialized = LE_FALSE;
}

leWidget* screenGetRoot_Screen0(uint32_t lyrIdx)
{
    if(lyrIdx >= LE_LAYER_COUNT)
        return NULL;

    switch(lyrIdx)
    {
        case 0:
        {
            return root0;
        }
        default:
        {
            return NULL;
        }
    }
}

